//
//  WorkRepaireController.swift
//  vmes
//
//  Created by 志鹏 翟 on 2022/3/5.
//

import Foundation
import UIKit
import Alamofire

class WorkRepaireController: BaseController {
    
    let topView=OrderDetailView()
    var orderInfo:OrderItem?
    
    let planNum=UILabel()
    let alreadyNum=UILabel()
    let waitNum=UILabel()
    
    //创建textField
    let textField_baogong = UITextField()
    let textField_buliang = UITextField()
    let textField_remark = UITextField()
    
    let contentView=UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.tintColor = .black
        self.navigationItem.title="工单补录"
        self.view.backgroundColor = .white
        
        let scrollview=UIScrollView()
        
        view.addSubview(scrollview)
        scrollview.addSubview(contentView)
        scrollview.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(scrollview)
            make.width.equalTo(view)
        }
        
        contentView.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(statusBarHeight)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(284)
        }
        topView.initData(orderInfo: orderInfo)
        initNumLabel()
        initInputArea()
    }
    
    func initNumLabel(){
        let planNumLb=UILabel()
        let allNumLb=UILabel()
        let waitNumLb=UILabel()
        planNumLb.text="总排产量"
        allNumLb.text="已报工量"
        waitNumLb.text="当班计划量"
        
        [planNumLb,allNumLb,waitNumLb].forEach({
            contentView.addSubview($0)
            $0.textAlignment = .center
            $0.textColor=UIColor.colorWithHex(hexStr: "#333333")
            $0.font = UIFont.systemFont(ofSize: 15)
        })
        planNumLb.snp.makeConstraints { make in
            make.width.equalTo(width/3)
            make.top.equalTo(topView.snp.bottom).offset(20)
        }
        allNumLb.snp.makeConstraints { make in
            make.width.equalTo(width/3)
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.left.equalTo(planNumLb.snp.right)
        }
        waitNumLb.snp.makeConstraints { make in
            make.width.equalTo(width/3)
            make.top.equalTo(topView.snp.bottom).offset(20)
            make.left.equalTo(allNumLb.snp.right)
        }
        [planNum,alreadyNum,waitNum].forEach({
            contentView.addSubview($0)
            $0.textAlignment = .center
            $0.text="500"
            $0.textColor=UIColor.colorWithHex(hexStr: "#0FA386")
            $0.font = UIFont.systemFont(ofSize: 20)
        })
        planNum.snp.makeConstraints { make in
            make.centerX.equalTo(planNumLb.snp.centerX)
            make.top.equalTo(planNumLb.snp.bottom).offset(5)
        }
        alreadyNum.snp.makeConstraints { make in
            make.centerX.equalTo(allNumLb.snp.centerX)
            make.top.equalTo(planNumLb.snp.bottom).offset(5)
        }
        waitNum.snp.makeConstraints { make in
            make.centerX.equalTo(waitNumLb.snp.centerX)
            make.top.equalTo(planNumLb.snp.bottom).offset(5)
        }
        planNum.text=String(orderInfo?.scheduleNumber ?? 0)
        alreadyNum.text=String(orderInfo?.workreportOutput ?? 0)
        waitNum.text=String(orderInfo?.shiftPlanOutput ?? 0)
    }
    
    func initInputArea(){
        let area=UIView()
        area.backgroundColor = UIColor.colorWithHex(hexStr: "#0FA386")
        contentView.addSubview(area)
        area.snp.makeConstraints { make in
            make.top.equalTo(planNum.snp.bottom).offset(20)
            make.left.right.equalToSuperview()
            make.height.equalTo(500)
            make.bottom.equalTo(contentView)
        }
        let inner=UIView()
        inner.layer.cornerRadius=10
        inner.backgroundColor = .white
        area.addSubview(inner)
        inner.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(20)
            make.bottom.equalToSuperview().offset(-40)
        }
        let numArea=UIView()
        numArea.backgroundColor = UIColor.colorWithHex(hexStr: "#E3E5E7")
        let tongkaiLB=UILabel()
        let zidongLB=UILabel()
        tongkaiLB.textAlignment = .center
        zidongLB.textAlignment = .center
        tongkaiLB.text="同开工量："+String(orderInfo?.workreportOutputCount ?? 0)
        zidongLB.text="自动总量："+String(orderInfo?.acqOutputCount ?? 0)
        numArea.addSubview(tongkaiLB)
        numArea.addSubview(zidongLB)
        
        inner.addSubview(numArea)
        numArea.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(30)
        }
        let width1:Int=width/2-35
        tongkaiLB.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.width.equalTo(width1)
            make.height.equalToSuperview()
            
        }
        zidongLB.snp.makeConstraints { make in
            make.left.equalTo(tongkaiLB.snp.right)
            make.width.equalTo(width1)
            make.height.equalToSuperview()
        }
        [textField_baogong,textField_buliang,textField_remark].forEach({
            inner.addSubview($0)
            $0.textAlignment = .right
            $0.text="0"
            $0.keyboardType = .decimalPad
            $0.textColor=UIColor.colorWithHex(hexStr: "#0FA386")
            $0.font = UIFont.systemFont(ofSize: 20)
            $0.leftViewMode = .always
        })
        textField_remark.keyboardType = .default
        textField_remark.text=""
        let label_baogong=UILabel()
        label_baogong.text="报工数量"
        textField_baogong.leftView = label_baogong
        let line1=UIView()
        line1.backgroundColor = UIColor.colorWithHex(hexStr: "#6F6F6F")
        inner.addSubview(line1)
        line1.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(1)
            make.top.equalTo(textField_baogong.snp.bottom)
        }
        textField_baogong.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(40)
            make.top.equalTo(numArea.snp.bottom).offset(10)
        }
        
        let label=UILabel()
        label.text="不良品数"
        textField_buliang.leftView = label
        
        let label2=UILabel()
        label2.text="备注"
        textField_remark.leftView = label2
        textField_buliang.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(40)
            make.top.equalTo(textField_baogong.snp.bottom).offset(20)
        }
        let line=UIView()
        line.backgroundColor = UIColor.colorWithHex(hexStr: "#6F6F6F")
        inner.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(1)
            make.top.equalTo(textField_buliang.snp.bottom)
        }
        textField_remark.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(40)
            make.top.equalTo(line.snp.bottom).offset(20)
        }
        let line2=UIView()
        line2.backgroundColor = UIColor.colorWithHex(hexStr: "#6F6F6F")
        inner.addSubview(line2)
        line2.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(1)
            make.top.equalTo(textField_remark.snp.bottom)
        }
        let btn=UIButton()
        btn.layer.cornerRadius=5
        inner.addSubview(btn)
        btn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalTo(line2).offset(30)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(55)
        }
        btn.setTitle("提交补录信息", for: .normal)
        btn.backgroundColor = UIColor.colorWithHex(hexStr: "#FD8D19")
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btn.setTitleColor(UIColor.colorWithHex(hexStr: "#ffffff"), for: .normal)
        btn.addTarget(self, action: #selector(showConfrimAlert), for: .touchUpInside)
    }
    
    @objc func showConfrimAlert(){
        let alertView = UIAlertController.init(title: "提示", message: "确认补录么", preferredStyle: .alert)
        let alert = UIAlertAction.init(title: "确定", style: .destructive) { (UIAlertAction) in
            self.finishWork()
        }
        let cancleAlert = UIAlertAction.init(title: "取消", style: .cancel) { (UIAlertAction) in
            print("点击取消按钮")
        }
        alertView.addAction(cancleAlert)
        alertView.addAction(alert);
        self.present(alertView, animated: true, completion: nil)
    }
    
    @objc func finishWork(){
        let abc:String=String(orderInfo?.tbBShiftWork?.isAutoWork ?? 0)
        spinner.startAnimating()
        let parameters: Dictionary = [
            "compId" : K.userInfo?.company?.companyId!,
            "recordUser" : K.userInfo?.userId!,
            "workorderCode" : orderInfo?.workorderCode!,
            "workshopId" : K.currentWorkShop?.workshopId!,
            "scheduleId":orderInfo?.id!,
            "batchType":abc,
            "workType":orderInfo?.tbBShiftWork?.workType!,
            "workreportOutput" : textField_baogong.text ?? "",
            "workreportRejectNumber" : textField_buliang.text ?? "",
            "remarkInfo" : textField_remark.text ?? ""
        ]
        AF.request(NetInfo.repaireWork, method:.put
                   , parameters:parameters,encoder: JSONParameterEncoder.default,headers: K.headers!,
                   requestModifier: { $0.timeoutInterval = 10 }).responseJSON { (response) in
            self.spinner.stopAnimating()
            print(response)
            switch response.result {
            case .success:
                if let data=response.data{
                    let response = try? JSONDecoder().decode(LoginInfo.self, from: data)
                    if(response?.code==200){
                        self.view.makeToast("补录成功",duration:2)
                        self.perform(#selector(self.backItemPressed), with: nil, afterDelay: 2)
                    }else{
                        self.view.makeToast(response?.msg)
                    }
                }
            case .failure:
                print("ERROR")
                self.view.makeToast("请求失败")
            }
        }
    }
    
}
